/*
  Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>
  
  This file is part of Vos Tutos Offline.
  
  Vos Tutos Offline is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Vos Tutos Offline is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mainwindow.h"

#include "alert.h"
#include "document.h"

/* Qt Librairies */

#include <QObject>
#include <QInputDialog>
#include <QString>
#include <QToolBar>

void VTOMainWindow::editionLayout()
{
    if(tutorial != NULL)
    {
	tutotree = new TutoTree(tutorial, tutorialtype);

	connect(tutotree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this,
		SLOT(editionPartChanged(QTreeWidgetItem*, int)));
			
	connect(tutotree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this,
		SLOT(partTitleEditionRequest(QTreeWidgetItem*, int)));

	connect(nForm->getEdNcode()->document(), SIGNAL(contentsChanged()),
		this, SLOT(documentWasModified()));

        connect(nForm->licence, SIGNAL(activated(int)),
                this, SLOT(licenceChange(int)));
        connect(nForm->progress, SIGNAL(valueChanged(int)),
                this, SLOT(progressChange(int)));

        connect(nForm->difficult, SIGNAL(activated(int)),
                this, SLOT(difficultChange(int)));

        connect(nForm->title_te, SIGNAL(textEdited(QString)),
                this, SLOT(titleChange(QString)));

	tutotreeDock->setWidget(tutotree);
	nForm->setEnabled(true);

	vtoMenuBar->updateMenu(tutorialtype);
    }
}


/* When no editing */
void VTOMainWindow::clearLayout()
{		
    tutotreeDock->setWidget(NULL);
    nForm->clear();
    nForm->setEnabled(false);
    statusBar->showMessage(tr("Start or open a document to write"));
    setWindowModified(false);
}


void VTOMainWindow::editionMode(TitledDocument* tuto, int tutot)
{
    setWindowModified(false);
    statusBar->showMessage(tr("Ready"));
    
    if (!tuto) return;
    tutorial = tuto; tutorialtype = tutot;
    docSelected = tuto; docSelectedType = tutot;
    editionLayout();
    
    setWindowTitle(QString( tuto->getTitle() + "[*] - " + tr("Vos Tutos Offline", "title") ));

    nForm->licence->setCurrentIndex(tuto->getLicence());
}


QDockWidget* VTOMainWindow::getTutoTreeDock()
{
    return tutotreeDock;
}

void VTOMainWindow::editionPartChanged(QTreeWidgetItem* it, int col)
{
    setWindowModified(false);
    
    if(it->type() == TutoTree::INTRO || it->type() == TutoTree::CONCLU)
    {
	ICTreeItem* tti = (ICTreeItem*)it;
        nForm->setEditingPart((Part*) tti->getParent()->getPart(it->type()));
        nForm->title_te->setText(QString(""));
        nForm->title_te->setEnabled((false));
        nForm->hideTitledOptions();
    }
    else if(it->type() == TutoTree::NEWS)
    {
        TutoTreeItem* tti = (TutoTreeItem*)it;
        docSelected = tti->getPart();
        docSelectedType = it->type();

        nForm->setEditingPart((Part*)tti->getPart());
        nForm->title_te->setEnabled((true));
        nForm->title_te->setText(docSelected->getTitle());
        nForm->hideTitledOptions();
    }
    else
    {
	TutoTreeItem* tti = (TutoTreeItem*)it;
	docSelected = tti->getPart();
        docSelectedType = it->type();

        nForm->title_te->setEnabled((true));
        nForm->difficult->setCurrentIndex(docSelected->getDifficult());
        nForm->progress->setValue(docSelected->getProgress());
        nForm->title_te->setText(docSelected->getTitle());
	vtoMenuBar->updateMenu(it->type());
        nForm->displayTitleOptions();
	nForm->setEditingPart(NULL);
    }
}

void VTOMainWindow::partTitleEditionRequest(QTreeWidgetItem* it, int col)
{
    if(it->type() == TutoTree::INTRO  || it->type() == TutoTree::CONCLU)
	return;
		
    TutoTreeItem* tti = (TutoTreeItem*)it;
    QString prompt = tr("Please enter the new part title :");
    bool ok;
    QString text = QInputDialog::getText(this, tr("Title edition"), prompt,
					 QLineEdit::Normal, "", &ok);
    if(ok)
    {
        tti->getPart()->setTitle(text);
        tti->setText(0, text);
        setWindowModified(true);
    }
}

/* Move and Delete selected */

void VTOMainWindow::upSelected()
{
    moveSelected(true);
}

void VTOMainWindow::downSelected()
{
    moveSelected(false);
}

void VTOMainWindow::moveSelected(bool up)
{
    if(tutorial == NULL)
        return;
    try{
	QTreeWidgetItem *qselected = tutotree->currentItem();
	if(qselected->type() == VTO::INTRO || qselected->type() == VTO::CONCLU)
	    throw 105;

	TutoTreeItem* selected = static_cast<TutoTreeItem*>(qselected);
		
	if(selected->getType() == VTO::NEWS && (tutorialtype == VTO::MINITUTORIAL || tutorialtype == VTO::BIGTUTORIAL))
	{
	    TutoTreeItem* itemparent = static_cast<TutoTreeItem*>(selected->parent());
	    MiniTutorial* parent = static_cast<MiniTutorial*>(itemparent->getPart());
			
	    int index = parent->allParts().indexOf(static_cast<Part*>(selected->getPart()));
	    if(up && index > 0) parent->allParts().swap(index, index-1);
	    else if(!up && index < (parent->allParts().size() -1)) parent->allParts().swap(index, index+1);
	    else throw 105;
	}
	else if(selected->getType() == VTO::MINITUTORIAL && tutorialtype == VTO::BIGTUTORIAL)
	{		
	    TutoTreeItem* itemparent = static_cast<TutoTreeItem*>(selected->parent());
	    BigTutorialPart* parent = static_cast<BigTutorialPart*>(itemparent->getPart());
			
	    int index = parent->allChapters().indexOf(static_cast<MiniTutorial*>(selected->getPart()));
	    if(up && index > 0) parent->allChapters().swap(index, index-1);
	    else if(!up && index < (parent->allChapters().size() -1)) parent->allChapters().swap(index, index+1);
	    else throw 105;
	}
	else if(selected->getType() == VTO::CHAPTER && tutorialtype == VTO::BIGTUTORIAL)
	{
	    TutoTreeItem* itemparent = static_cast<TutoTreeItem*>(selected->parent());
	    BigTutorial* parent = static_cast<BigTutorial*>(itemparent->getPart());
		
	    int index = parent->allParts().indexOf(static_cast<BigTutorialPart*>(selected->getPart()));
	    if(up && index > 0) parent->allParts().swap(index, index-1);
	    else if(!up && index < (parent->allParts().size() -1)) parent->allParts().swap(index, index+1);
	    else throw 105;
        }

        tutotree->refresh();
	
    }catch(int e){
	VTOAlert::alert(e);
    }
}

void VTOMainWindow::removeSelected()
{
    if(tutorial == NULL)
        return;
    try{
        QTreeWidgetItem *qselected = tutotree->currentItem();
        if(qselected->type() == VTO::INTRO || qselected->type() == VTO::CONCLU)
            throw 106;

        TutoTreeItem* selected = static_cast<TutoTreeItem*>(qselected);

        if(selected->getType() == VTO::NEWS && (tutorialtype == VTO::MINITUTORIAL || tutorialtype == VTO::BIGTUTORIAL))
        {
            TutoTreeItem* itemparent = static_cast<TutoTreeItem*>(selected->parent());
            MiniTutorial* parent = static_cast<MiniTutorial*>(itemparent->getPart());

            int index = parent->allParts().indexOf(static_cast<Part*>(selected->getPart()));
            delete static_cast<Part*>(selected->getPart());
            parent->allParts().removeAt(index);
        }
        else if(selected->getType() == VTO::MINITUTORIAL && tutorialtype == VTO::BIGTUTORIAL)
        {
            TutoTreeItem* itemparent = static_cast<TutoTreeItem*>(selected->parent());
            BigTutorialPart* parent = static_cast<BigTutorialPart*>(itemparent->getPart());

            int index = parent->allChapters().indexOf(static_cast<MiniTutorial*>(selected->getPart()));
            delete static_cast<MiniTutorial*>(selected->getPart());
            parent->allChapters().removeAt(index);
        }
        else if(selected->getType() == VTO::CHAPTER && tutorialtype == VTO::BIGTUTORIAL)
        {
            TutoTreeItem* itemparent = static_cast<TutoTreeItem*>(selected->parent());
            BigTutorial* parent = static_cast<BigTutorial*>(itemparent->getPart());

            int index = parent->allParts().indexOf(static_cast<BigTutorialPart*>(selected->getPart()));
            delete static_cast<BigTutorialPart*>(selected->getPart());
            parent->allParts().removeAt(index);

        }

        tutotree->refresh();

    }catch(int e){
        VTOAlert::alert(e);
    }

}


void VTOMainWindow::documentWasModified()
{
    setWindowModified(nForm->getEdNcode()->document()->isModified());
}
